home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / GIFWEDGE.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  6KB  |  169 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to create a test image of White and Red/Green/Blue levels for      *
  7. * test purposes. The Primary (RGB) and Secondary (YCM) are also displayed.   *
  8. * background.                                     *
  9. * Options:                                     *
  10. * -q : quite printing mode.                             *
  11. * -s Width Height : set image size.                         *
  12. * -l levels : number of color levels.                         *
  13. * -h : on line help.                                 *
  14. ******************************************************************************
  15. * History:                                     *
  16. * 4 Jan 90 - Version 1.0 by Gershon Elber.                     *
  17. *****************************************************************************/
  18.  
  19. #ifdef __MSDOS__
  20. #include <stdlib.h>
  21. #include <alloc.h>
  22. #endif /* __MSDOS__ */
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include "gif_lib.h"
  28. #include "getarg.h"
  29.  
  30. #define PROGRAM_NAME    "GifWedge"
  31.  
  32. #define DEFAULT_WIDTH    640
  33. #define DEFAULT_HEIGHT    350
  34.  
  35. #define DEFAULT_NUM_LEVELS    16     /* Number of colors to gen the image. */
  36.  
  37. #ifdef __MSDOS__
  38. extern unsigned int
  39.     _stklen = 16384;                 /* Increase default stack size. */
  40. #endif /* __MSDOS__ */
  41.  
  42. #ifdef SYSV
  43. static char *VersionStr =
  44.         "Gif library module,\t\tGershon Elber\n\
  45.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  46. static char
  47.     *CtrlStr = "GifWedge q%- l%-#Lvls!d s%-Width|Height!d!d h%-";
  48. #else
  49. static char
  50.     *VersionStr =
  51.     PROGRAM_NAME
  52.     GIF_LIB_VERSION
  53.     "    Gershon Elber,    "
  54.     __DATE__ ",   " __TIME__ "\n"
  55.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  56. static char
  57.     *CtrlStr =
  58.     PROGRAM_NAME
  59.     " q%- l%-#Lvls!d s%-Width|Height!d!d h%-";
  60. #endif /* SYSV */
  61.  
  62. static int
  63.     NumLevels = DEFAULT_NUM_LEVELS,
  64.     ImageWidth = DEFAULT_WIDTH,
  65.     ImageHeight = DEFAULT_HEIGHT;
  66.  
  67. static void QuitGifError(GifFileType *GifFile);
  68.  
  69. /******************************************************************************
  70. * Interpret the command line and scan the given GIF file.              *
  71. ******************************************************************************/
  72. void main(int argc, char **argv)
  73. {
  74.     int    i, j, l, c, Error, LevelStep, LogNumLevels,
  75.     Count = 0, LevelsFlag = FALSE, SizeFlag = FALSE, HelpFlag = FALSE;
  76.     GifRowType Line;
  77.     GifColorType *ColorMap;
  78.     GifFileType *GifFile;
  79.  
  80.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  81.         &GifQuitePrint, &LevelsFlag, &NumLevels,
  82.         &SizeFlag, &ImageWidth, &ImageHeight,
  83.         &HelpFlag)) != FALSE) {
  84.     GAPrintErrMsg(Error);
  85.     GAPrintHowTo(CtrlStr);
  86.     exit(1);
  87.     }
  88.  
  89.     if (HelpFlag) {
  90.     fprintf(stderr, VersionStr);
  91.     GAPrintHowTo(CtrlStr);
  92.     exit(0);
  93.     }
  94.  
  95.     /* Make sure the number of levels is power of 2 (up to 32 levels.). */
  96.     for (i = 1; i < 6; i++) if (NumLevels == (1 << i)) break;
  97.     if (i == 6) GIF_EXIT("#Lvls (-l option) is not power of 2 up to 32.");
  98.     LogNumLevels = i + 3;               /* Multiple by 8 (see below). */
  99.     LevelStep = 256 / NumLevels;
  100.  
  101.     /* Make sure the image dimension is a multiple of NumLevels horizontally */
  102.     /* and 7 (White, Red, Green, Blue and Yellow Cyan Magenta) vertically.   */
  103.     ImageWidth = (ImageWidth / NumLevels) * NumLevels;
  104.     ImageHeight = (ImageHeight / 7) * 7;
  105.  
  106.     /* Open stdout for the output file: */
  107.     if ((GifFile = EGifOpenFileHandle(1)) == NULL)
  108.     QuitGifError(GifFile);
  109.  
  110.     /* Dump out screen description with given size and generated color map:  */
  111.     /* The color map has 7 NumLevels colors for White, Red, Green and then   */
  112.     /* The secondary colors Yellow Cyan and magenta.                 */
  113.     if ((ColorMap = (GifColorType *)
  114.         malloc(8 * NumLevels * sizeof(GifColorType))) == NULL)
  115.     GIF_EXIT("Failed to allocate memory required, aborted.");
  116.  
  117.     for (i = 0; i < 8; i++)                   /* Set color map. */
  118.     for (j = 0; j < NumLevels; j++) {
  119.         l = LevelStep * j;
  120.         c = i * NumLevels + j;
  121.         ColorMap[c].Red = (i == 0 || i == 1 || i == 4 || i == 6) * l;
  122.         ColorMap[c].Green =    (i == 0 || i == 2 || i == 4 || i == 5) * l;
  123.         ColorMap[c].Blue = (i == 0 || i == 3 || i == 5 || i == 6) * l;
  124.     }
  125.  
  126.     if (EGifPutScreenDesc(GifFile,
  127.     ImageWidth, ImageHeight, LogNumLevels, 0, LogNumLevels, ColorMap)
  128.     == GIF_ERROR)
  129.     QuitGifError(GifFile);
  130.  
  131.     /* Dump out the image descriptor: */
  132.     if (EGifPutImageDesc(GifFile,
  133.     0, 0, ImageWidth, ImageHeight, FALSE, LogNumLevels, NULL) == GIF_ERROR)
  134.     QuitGifError(GifFile);
  135.  
  136.     GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
  137.             PROGRAM_NAME, GifFile -> ILeft, GifFile -> ITop,
  138.             GifFile -> IWidth, GifFile -> IHeight);
  139.  
  140.     /* Allocate one scan line to be used for all image.                 */
  141.     if ((Line = (GifRowType) malloc(sizeof(GifPixelType) * ImageWidth)) == NULL)
  142.     GIF_EXIT("Failed to allocate memory required, aborted.");
  143.  
  144.     /* Dump the pixels: */
  145.     for (c = 0; c < 7; c++) {
  146.     for (i = 0, l = 0; i < NumLevels; i++)
  147.         for (j = 0; j < ImageWidth / NumLevels; j++)
  148.         Line[l++] = i + NumLevels * c;
  149.     for (i = 0; i < ImageHeight / 7; i++) {
  150.         if (EGifPutLine(GifFile, Line, ImageWidth) == GIF_ERROR)
  151.         QuitGifError(GifFile);
  152.         GifQprintf("\b\b\b\b%-4d", Count++);
  153.     }
  154.     }
  155.  
  156.     if (EGifCloseFile(GifFile) == GIF_ERROR)
  157.     QuitGifError(GifFile);
  158. }
  159.  
  160. /******************************************************************************
  161. * Close output file (if open), and exit.                      *
  162. ******************************************************************************/
  163. static void QuitGifError(GifFileType *GifFile)
  164. {
  165.     PrintGifError();
  166.     if (GifFile != NULL) DGifCloseFile(GifFile);
  167.     exit(1);
  168. }
  169.